<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use think\Request;
use think\Session;
use think\paginator\driver\Page;
class Article extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';

    public function index()
    {
        $num =7; //分页个数
        $page = input("get.page") ? input("get.page") : 1; //当前页
        $limit=($page-1)*$num;
        $id=input("get.tag"); 
        $category=db('category')->field('id,name')->where('id',$id)->find();
        $this->view->assign("category",$category);
        $where['category_id']=$id;
        $table = db("article");
        //左边图片
        $whereleft['category_id']=15;
        $whereleft['image']=array('neq','');
        $whereleft['ztsh']='1';
        $leftlist=$table->where($whereleft)->limit(4)->select();
       //右边列表
        $whereright['category_id']=$id;
        $whereright['ztsh']='1';
        $trategy_num=count($table->where($whereright)->select());
        $ztsh='1';
        $sql = 'select * from zc_article where ztsh="'.$ztsh.'" and category_id ='.$id;
        $list = db()->query($sql." order by weigh desc LIMIT {$limit},{$num}");
        $data = Page::make($list, $num, $page, $trategy_num, false, ['path' => Page::getCurrentPath(), 'query' => request()->param()]);
        $this->view->assign("leftlist",$leftlist);
        $this->view->assign("data",$data);
        return $this->view->fetch();

    }
    public function detail()
    {
        $id=input("get.id"); 
        $category_id=input("get.cid"); 
        $category=db('category')->field('id,name')->where('id',$category_id)->find();
        $this->view->assign("category",$category);


        $table = db("article");
        //左边图片
        $whereleft['category_id']=15;
        $whereleft['image']=array('neq','');
        $whereleft['ztsh']='1';
        $leftlist=$table->where($whereleft)->limit(4)->select();
        $this->view->assign("leftlist",$leftlist);
        $show= $table->where("id=".$id)->find();
        $this->view->assign("show",$show);
        $ids = $table->where('category_id',$category_id)->column('id');
        foreach ($ids as $k => $v) {
            if($id==$ids[$k]){
                  //定位当篇
                  //获取下一篇
                  if(isset($ids[$k+1])){
                      $next_id = $ids[$k+1];
                  }else{
                      $next_id = '';
                  }
                  //获取上一篇
                  if(isset($ids[$k-1])){
                      $pre_id = $ids[$k-1];
                  }else{
                      $pre_id = '';
                  }
            }
        }
        if($next_id != ''){
        	$next_artilce = $table->where('id',$next_id)->field('id,category_id,title')->find();
        }else{
        	$next_artilce = '';
        }

        if($pre_id !=''){
        	$pre_article = $table->where('id',$pre_id)->field('id,category_id,title')->find();
        }else{
        	$pre_article = '';
        }
     
        $this->view->assign("pre_article",$pre_article);
        $this->view->assign("next_artilce",$next_artilce);
        return $this->view->fetch();
    }
    
}
